package com.lingCheng.avg.service.impl;


import com.lingCheng.avg.common.JiaDongData;
import com.lingCheng.avg.common.JiaDongValue;
import com.lingCheng.avg.entity.WcsTrkHis;
import com.lingCheng.avg.mapper.WcsTrkHisMapper;
import com.lingCheng.avg.service.WcsTrkHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author 吕继锋
* @description 针对表【wcs_trk_his(任务信息)】的数据库操作Service实现
* @createDate 2023-12-19 14:44:15
*/
@Service
public class WcsTrkHisServiceImpl implements WcsTrkHisService {
    @Autowired
    private WcsTrkHisMapper wcsTrkHisMapper;

    @Override
    public JiaDongData findAllRW() {
        List<WcsTrkHis> allRW = wcsTrkHisMapper.findAllRW();
        Set<String> timeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String time1, String time2) {
                // 自定义比较器，按照日期大小进行排序
                int hour1 = Integer.parseInt(time1);
                int hour2 = Integer.parseInt(time2);
                return Integer.compare(hour1, hour2);
            }
        });
        Map<String, List<Integer>> deviceDataMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String deviceName1, String deviceName2) {
                // 自定义比较器，按照设备名称的顺序进行排序
                int deviceNum1 = getDeviceNumber(deviceName1);
                int deviceNum2 = getDeviceNumber(deviceName2);
                return Integer.compare(deviceNum1, deviceNum2);
            }

            private int getDeviceNumber(String deviceName) {
                // 提取设备名称中的数字部分
                String numberPart = deviceName.replaceAll("\\D+", "");
                return Integer.parseInt(numberPart);
            }
        });

        // 遍历数据，构建timeSet和deviceDataMap
        for (WcsTrkHis wcsTrkHis : allRW) {
            if (wcsTrkHis.getDeviceName() != null && !wcsTrkHis.getDeviceName().isEmpty() && !wcsTrkHis.getDeviceName().equals("")) {
                timeSet.add(wcsTrkHis.getTime());  // 添加时间到TreeSet中
                String deviceName = wcsTrkHis.getDeviceName();
                int deviceNum = Integer.parseInt(deviceName);

                // 按照设备编号生成设备名称
                switch (deviceNum) {
                    case 1:
                        deviceName = "1号堆垛机";
                        break;
                    case 2:
                        deviceName = "2号堆垛机";
                        break;
                    case 3:
                        deviceName = "3号堆垛机";
                        break;
                    case 4:
                        deviceName = "4号堆垛机";
                        break;
                    case 5:
                        deviceName = "5号堆垛机";
                        break;
                    case 6:
                        deviceName = "6号堆垛机";
                        break;
                    default:
                        deviceName = "未知堆垛机";
                        break;
                }

                if (deviceDataMap.containsKey(deviceName)) {
                    deviceDataMap.get(deviceName).add(wcsTrkHis.getData());
                } else {
                    List<Integer> dataList = new ArrayList<>();
                    dataList.add(wcsTrkHis.getData());
                    deviceDataMap.put(deviceName, dataList);
                }
            }
        }

        // 构建time列表并按日期大小排序
        List<String> time = new ArrayList<>(timeSet);

        // 构建value列表
        List<JiaDongValue> value = new ArrayList<>();
        for (Map.Entry<String, List<Integer>> entry : deviceDataMap.entrySet()) {
            String deviceName = entry.getKey();
            List<Integer> data = entry.getValue();

            // 构建JiaDongValue对象
            JiaDongValue jiaDongValue = new JiaDongValue();
            jiaDongValue.setDeviceName(deviceName);
            jiaDongValue.setData(data.toArray(new Integer[0]));
            value.add(jiaDongValue);
        }

        JiaDongData data = new JiaDongData();
        data.setTime(time.toArray(new String[0]));
        data.setValue(value);
        return data;
    }
}




